package com.tangyee.person;

import android.content.ContentValues;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.tangyee.person.db.BaseInfo;
import com.tangyee.person.dialogfragment.SaveDialogFragment;
import com.tangyee.R;
import com.tangyee.person.db.BaseInfo;
import com.tangyee.person.dialogfragment.NewDialogFragment;
import com.tangyee.person.dialogfragment.NoticeDialogFragment;
import com.tangyee.person.dialogfragment.SaveDialogFragment;
import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BasicInfomationActivity extends AppCompatActivity implements View.OnClickListener,SaveDialogFragment.DialogListener,NewDialogFragment.DialogListener{
    private  TextView sex;
    private  TextView birth;
    private  TextView high;
    private  TextView weight;
    private  TextView bmi;
    private List<BaseInfo> baseInfos;
    private double double_height;
    private double double_weight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_person_basicinformation);
        findViewById(R.id.backToperson).setOnClickListener(this);
        findViewById(R.id.save).setOnClickListener(this);
        findViewById(R.id.sex).setOnClickListener(this);
        findViewById(R.id.birth).setOnClickListener(this);
        findViewById(R.id.high).setOnClickListener(this);
        findViewById(R.id.weight).setOnClickListener(this);
        sex=findViewById(R.id._sex);
        birth=findViewById(R.id._birth);
        high=findViewById(R.id._high);
        bmi=findViewById(R.id._bmi);
        weight=findViewById(R.id._weight);
        bmi=findViewById(R.id._bmi);
//        Connector.getDatabase();//创建数据库
        baseInfos = LitePal.findAll(BaseInfo.class);
        setDate();
        if (baseInfos.isEmpty()) {
            DialogFragment dialogFragment = new NewDialogFragment();
            dialogFragment.show(getSupportFragmentManager(), "");
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.backToperson:
                baseInfos = LitePal.findAll(BaseInfo.class);
                if (baseInfos.isEmpty() == false) {
                    BaseInfo baseInfo = LitePal.find(BaseInfo.class, 1);
                    //如果有某项数据修改了话，提醒是否保存
                    if (sex.getText().toString().equals(baseInfo.getSex()) == false
                            || birth.getText().toString().equals(baseInfo.getBirth()) == false
                            || high.getText().toString().equals(baseInfo.getHigh()) == false
                            || weight.getText().toString().equals(baseInfo.getWeight()) == false
                            || bmi.getText().toString().equals(baseInfo.getBMI()) == false) {
                        SaveDialogFragment saveDialogFragment = new SaveDialogFragment();
                        saveDialogFragment.show(getSupportFragmentManager(), "");
                    } else {
                        finish();
                    }
                } else {
                    finish();
                }
                break;
            case R.id.save:
                //保存
                if (TextUtils.isEmpty(sex.getText()) == false
                        && TextUtils.isEmpty(birth.getText()) == false
                        && TextUtils.isEmpty(high.getText()) == false
                        && TextUtils.isEmpty(weight.getText()) == false
                        && TextUtils.isEmpty(bmi.getText()) == false) {
                    baseInfos = LitePal.findAll(BaseInfo.class);
                    if (baseInfos.isEmpty()) {
                        //数据库为空则添加数据
                        //否则就修改id为11的那条数据
                        getBaseInfo();
                    } else {
                        updateBaseInfo();
                    }
                    finish();
                } else {
                    NoticeDialogFragment noticeDialogFragment = new NoticeDialogFragment();
                    noticeDialogFragment.show(getSupportFragmentManager(), "");
                }
                break;
            case R.id.sex:
                //性别
                final ArrayList<String> sex1 = new ArrayList<>();
                sex1.add("男");
                sex1.add("女");
                OptionsPickerView optionsPickerView = new OptionsPickerBuilder(BasicInfomationActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String tx = sex1.get(options1);
                        sex.setText(tx);
                    }
                }).build();
                optionsPickerView.setPicker(sex1);
                optionsPickerView.show();
                break;
            case R.id.birth:
                //出生日期
                TimePickerView timePickerView = new TimePickerBuilder(BasicInfomationActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date1, View v) {
                        //将日期格式转化为yyyy-MM-dd格式
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String date2 = simpleDateFormat.format(date1);
                        birth.setText(date2);
                    }
                }).build();
                timePickerView.show();
                break;
            case R.id.high:
                //身高
                //构造数据
                final ArrayList<String> heights1 = new ArrayList<>();
                final ArrayList<String> heights2 = new ArrayList<>();
                final ArrayList<String> heights3 = new ArrayList<>();
                heights3.add("cm");
                for (int i = 130; i <= 200; i++) {
                    String t = String.valueOf(i);
                    heights1.add(t);
                }
                for (int i = 0; i <= 9; i++) {
                    String a = "." + i;
                    heights2.add(a);
                }
                OptionsPickerView optionsPickerView1 = new OptionsPickerBuilder(BasicInfomationActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String text = heights1.get(options1) + heights2.get(options2) + heights3.get(options3);
                        high.setText(text);
                        sum();
                    }
                }).build();
                //添加数据源
                optionsPickerView1.setNPicker(heights1, heights2, heights3);
                optionsPickerView1.setSelectOptions(40, 0, 0);
                optionsPickerView1.show();
                break;
            case R.id.weight:
                //体重
                final ArrayList<String> weights1 = new ArrayList<>();
                final ArrayList<String> weights2 = new ArrayList<>();
                final ArrayList<String> weights3 = new ArrayList<>();
                for (int i = 40; i <= 200; i++) {
                    String t = String.valueOf(i);
                    weights1.add(t);
                }
                for (int i = 0; i <= 9; i++) {
                    String a = "." + i;
                    weights2.add(a);
                }
                weights3.add("kg");
                OptionsPickerView optionsPickerView2 = new OptionsPickerBuilder(BasicInfomationActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String text = weights1.get(options1) + weights2.get(options2) + weights3.get(options3);
                        weight.setText(text);
                        sum();
                    }
                }).build();
                optionsPickerView2.setNPicker(weights1, weights2, weights3);
                optionsPickerView2.setSelectOptions(10, 0, 0);
                optionsPickerView2.show();
                break;
        }
    }
    //确定按钮监听
    @Override
    public void onDialogPositiveClick(DialogFragment dialogFragment) {
        if (baseInfos.isEmpty()==false) {
            updateBaseInfo();
            finish();
        }
    }
    //取消按钮监听
    @Override
    public void onDialogNegativeClick(DialogFragment dialogFragment) {
        finish();
    }
    //将数据库的数据添加到textview中
    private void setDate() {
        baseInfos = LitePal.findAll(BaseInfo.class);
        if (baseInfos.isEmpty() == false) {
            BaseInfo baseInfo = LitePal.find(BaseInfo.class, 1);
            sex.setText(baseInfo.getSex());
            birth.setText(baseInfo.getBirth());
            high.setText(baseInfo.getHigh());
            weight.setText(baseInfo.getWeight());
            bmi.setText(baseInfo.getBMI());
        }
    }
    //去掉字符串中的字符
    private String getString(String s, String s1)//s是需要删除某个子串的字符串s1是需要删除的子串
    {
        int postion = s.indexOf(s1);
        int length = s1.length();
        int Length = s.length();
        String newString = s.substring(0,postion) + s.substring(postion + length, Length);
        return newString;//返回已经删除好的字符串
    }
    //计算BMI
    private void sum() {
        if (high.getText().toString().isEmpty() == false
                &&weight.getText().toString().isEmpty() == false) {
            String text1 = getString(high.getText().toString(), "cm");
            String text2 = getString(weight.getText().toString(), "kg");
            double_height = Double.parseDouble(text1);
            double_weight = Double.parseDouble(text2);
            double b = double_weight /((double_height /100)*(double_height /100));
            String BMI = new DecimalFormat("0.00").format(b);
            bmi.setText(BMI);
        }
    }
    //更新数据
    private void updateBaseInfo() {
        ContentValues values = new ContentValues();
        values.put("sex", sex.getText().toString());
        values.put("birth", birth.getText().toString());
        values.put("high", high.getText().toString());
        values.put("weight", weight.getText().toString());
        values.put("BMI", bmi.getText().toString());
        LitePal.update(BaseInfo.class, values, 1);
    }
    private void getBaseInfo() {
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setSex(sex.getText().toString());
        baseInfo.setBirth(birth.getText().toString());
        baseInfo.setHigh(high.getText().toString());
        baseInfo.setWeight(weight.getText().toString());
        baseInfo.setBMI(bmi.getText().toString());
        baseInfo.save();
    }
}
